<?php

namespace Admin\Controller;
//use Think\Controller;
use Tools\AdminController;
class GoodsController extends AdminController{
   function showlist1(){
       $goods = new \Model\GoodsModel();
       if($_GET['goods_id']){
           $goods ->delete($_GET['goods_id']);
       }
//       dump($goods);
     $info = $goods->order('goods_id desc')->select();
     $this->assign('info',$info);//将$db赋值给 info (tp框架封装方法)info->showlist
    
             
     $this->display();

   } 
   function showlist(){
       $goods = new \Model\GoodsModel();
       //删除商品
       if($_GET['goods_id']){
         $z = $goods ->delete($_GET['goods_id']);
//         if($z){
//             $this->success('成功!','../showlist',2);
//         }else{
//             $this->error('失败!','../showlist',2);
//         }
       }
    
    //分页
     //①获得总记录数目
     $total = $goods-> count();  // sum()  max()  min()
     $per = 5; //每页显示5条数据
     
     //② 实例化分页对象
     $page_obj = new \Tools\Page($total,$per);
     
     //③ 自定义sql语句,获得每页信息
     $sql = "select * from sw_goods order by goods_id desc ".$page_obj -> limit;
     $info = $goods -> query($sql);
     
     //④ 获得页码列表
     $pagelist = $page_obj -> fpage(array(3,4,5,6,7,8));
     
     $this -> assign('pagelist',$pagelist);
     $this -> assign('info',$info);
             
     $this->display();

   } 
   function tianjia1(){
       $goods = new \Model\GoodsModel();
       //数据添加
       //① 数组方式
//       $arr = array(
//           'goods_name'=> 三星s6,
//           'goods_price'=> 5500,
//           'goods_number'=> 121,
//           'goods_brand'=> 三星,
//           'goods_brand_id'=> 3,
//       );
//          $z = $goods -> add($arr);
//          dump($z);
       //② AR 方式
       //对象给本身不存在的属性进行赋值(包括给私有属性赋值)  __set()
//       $goods->goods_name = '小米3';
//       $goods->goods_price =999;
//       $goods->goods_number = 666;
//       $goods->goods_brand = '小米';
//       $goods->goods_brand_id = 4  ;  
//       $z = $goods -> add();
//       dump($z);
       $this->display();
   } 
   function tianjia(){
       $goods = D('Goods');
       if(!empty($_POST)){
           //上传图片
           if($_FILES['goods_big_img']['error']<4){
               $cfg = array(
                   'rootPath'  =>  'Public/uploadss/',
               );
               $up = new \Think\Upload($cfg);
               $pic = $up ->uploadOne($_FILES['goods_big_img']);
               $bigimg = $up ->rootPath.$pic['savepath'].$pic['savename'];
               $smallimg = $up ->rootPath.$pic['savepath'].'small_'.$pic['savename'];
               
               //对上传好的图片制作缩略图
               $img = new \Think\Image();
               $img ->open($bigimg);
               $img ->thumb(100, 100,6);
               $img ->save($smallimg);//保存缩略图到服务器
               //把上传好的附件储存到数据库里
               $_POST['goods_big_img'] = $bigimg;
               $_POST['goods_small_img'] = $smallimg;
           }
           $z = $goods-> add($_POST);
           if($z){
         //$this ->redirect([分组/控制器/操作方法]地址,参数,延迟时间,提示信息);
           $this ->redirect('showlist',array(),3,'添加商品成功!');
           }else{
           $this ->redirect('tianjia',array('name'=>$_POST['goods_name']),3,'添加商品失败!');
           }
       } else {
          $this->display();
       }
      }
   function xiugai($goods_id){
        $goods = new \Model\GoodsModel();
        if(!empty($_POST)){
            //修改图片
            if($_FILES['goods_big_img']['error']<4){
               $cfg = array(
                   'rootPath'  =>  'Public/uploadss/',
               );
               $up = new \Think\Upload($cfg);
               $pic = $up ->uploadOne($_FILES['goods_big_img']);
               $bigimg = $up ->rootPath.$pic['savepath'].$pic['savename'];
               $smallimg = $up ->rootPath.$pic['savepath'].'small_'.$pic['savename'];
               
               //对上传好的图片制作缩略图
               $img = new \Think\Image();
               $img ->open($bigimg);
               $img ->thumb(100, 100,6);
               $img ->save($smallimg);
               $_POST['goods_big_img'] = $bigimg;
               $_POST['goods_small_img'] = $smallimg;
           }
        $z = $goods->save($_POST);// where条件和主键信息 必须要有一个
         if($z){
         //$this ->redirect([分组/控制器/操作方法]地址,参数,延迟时间,提示信息);
           $this ->redirect('showlist',array(),3,'修改商品成功!');
           }else{
           $this ->redirect('xiugai',array('goods_id'=>$goods_id),3,'修改商品失败!');
           }
        }else{
           $info = $goods->find($goods_id);
           $this->assign('info',$info);
         $this->display();
        }

    //数组修改 必须要有 where 条件 或主键数据  否则修改失败
//       $goods->goods_id = 4;
//       $goods->goods_name = '魅蓝note3';
//       $goods->goods_price =1199;
//       $goods->goods_number = 800;
//       $goods->goods_brand = '魅族';
//       $goods->goods_brand_id = 5  ;  
//       $z = $goods->save();
//       dump($z);
   } 
   function shangjia($goods_id){
        $goods = D('Goods');
        $goods->where("goods_id = $goods_id")->save(array('goods_status'=>1));
        $this->redirect('showlist'); 
    }
    function xiajia($goods_id){
       $goods = D('Goods');
        $goods->where("goods_id = $goods_id")->save(array('goods_status'=>0));
        $this->redirect('showlist'); 
    }
}
